package com.panshi.controller.website;

import com.panshi.api.IAsmeApi;
import com.panshi.po.AsmePO;
import com.panshi.po.LinksPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class AsmeController {

    @Autowired
    IAsmeApi iAsmeApi;

    @GetMapping("asme/query")
    public String query(){
        return "asme/asme-query";
    }
    //关于我们查询
    @PostMapping("asmeQuery")
    @ResponseBody
    public PageModel<AsmePO> asmeQuery(PageModel<AsmePO> pm) {

        log.info("关于我们查询======{}", "*****************");

        pm.init();

        PageBeanAsmeVo pageBeanAsmeVo = new PageBeanAsmeVo();

        pageBeanAsmeVo.setCurrentPage(pm.getCurrentPage());

        pageBeanAsmeVo.setPageSize(pm.getPageSize());

        log.info("关于我们查询pageBeanAsmeVo============={}",pageBeanAsmeVo);

        log.info("pageBeanAsmeVo================={}", pageBeanAsmeVo);

        List<AsmePO> asmePOS=iAsmeApi.queryAsmePostBody(pageBeanAsmeVo).getData();

        Integer str=iAsmeApi.queryNumberAsmePostBody().getData();

        log.info("查询出来的数据====={}", asmePOS);

        pm.setList(asmePOS);

        pm.setCount(str);

        return pm;

    }

    @GetMapping("asmeAdd")
    public String asmeAdd() {
        return "asme/asme-add";
    }

    //关于我们增加
    @PostMapping("asmeAdd")
    @ResponseBody
    public PageModel asmeAdd(AddAsmeVO addLinksVO) {

        log.info("前端传来的数据======{}", addLinksVO);

        PageModel pageModel = new PageModel();

        addLinksVO.setCreatedate(new Date());

        ResponseVO str = iAsmeApi.addAsmePostBody(addLinksVO);

        Integer option= (Integer) str.getData();

        log.info("添加的返回来的参数==========={}",str);

        if (option == 1) {

            pageModel.setCode(10000);

            pageModel.setMsg(str.getMsg());

        } else if (option == 0) {

            pageModel.setCode(9999);

            pageModel.setMsg(str.getMsg());

        }
        return pageModel;
    }

    @GetMapping("asmeEdit")
    public String update(@RequestParam Integer id, Model model){
        log.info("编辑id==================={}",id);

        QueryIdVO queryIdVO=new QueryIdVO();

        queryIdVO.setId(id);

        AsmePO asmePO=iAsmeApi.queryIdAsmePostBody(queryIdVO).getData();

        log.info("通过id查询出来的数据============{}",asmePO);

        model.addAttribute("asmePO",asmePO);

        return  "asme/asme-edit";
    }

    //编辑修改
    @PostMapping("asme-edit")
    @ResponseBody
    public Map<String,Object> edit(UpdateAsmeVO updateLinksVO){

        log.info("修改前端传来的植====={}",updateLinksVO);

        Map<String,Object> map = new HashMap<>();

        updateLinksVO.setUpdatedate(new Date());

        ResponseVO str=iAsmeApi.updateAsmePostBody(updateLinksVO);

        Integer option= (Integer) str.getData();

        log.info("option=============={}",option);

        log.info("修改返回来的数据============={}",str);

        if(option==0){

            map.put("success",false);

            map.put("message",str.getMsg());

        }else if(option==1){

            map.put("success",true);

            map.put("message",str.getMsg());
        }
        return  map;
    }

    //删除方法
    @PostMapping("asme-del")
    @ResponseBody
    public Map<String,Object> del(DeleteAsmeVO deleteAsmeVO){

        log.info("删除传来的数据========{}",deleteAsmeVO);

        Map<String,Object> map = new HashMap<>();

        deleteAsmeVO.setUpdatedate(new Date());

        Integer state = iAsmeApi.deleteAsmePostBody(deleteAsmeVO).getData();

        if(state == 0){

            map.put("success",false);

            map.put("message","删除失败");

        }else if(state == 1){

            map.put("success",true);

            map.put("message","删除成功");

        }

        return map;

    }

    //模糊查询
    @PostMapping("asmeDimQuery/{vague}")
    @ResponseBody
    public PageModel<AsmePO> dimQuery(PageModel<AsmePO> pm, @PathVariable String  vague){

        pm.init();

        log.info("模糊查询前端传来的数据============={}",vague);

        LikeQueryAsmeVo likeQueryAsmeVo=new LikeQueryAsmeVo();

        likeQueryAsmeVo.setName(vague);

        likeQueryAsmeVo.setCurrentPage(pm.getCurrentPage());

        likeQueryAsmeVo.setPageSize(pm.getPageSize());

        List<AsmePO> list=iAsmeApi.queryDimAsmePostBody(likeQueryAsmeVo).getData();

        log.info("友情链接模糊查询出来的数据==============={}",list);

        Integer str=iAsmeApi.queryDimNumberAsmePostBody(vague).getData();

        log.info("查询出来的条数============{}",str);

        pm.setList(list);

        pm.setCount(str);

        return pm;

    }

}
